package com.chaodroid.flyui.utils;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

/**
 * Created by CHEN on 2018/1/22.
 */

public class DrawableUtils {

    /****************GradientDrawable = <sharp></sharp>********************/
    /**
     * 矩形
     *
     * @param bgcolor Color.parseColor("#FFFFFF");
     * @return
     */
    @SuppressLint("WrongConstant")
    public static GradientDrawable getRectangleDrawable(String bgcolor) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(Color.parseColor(bgcolor));// 设置颜色
        drawable.setGradientType(GradientDrawable.RECTANGLE);
        return drawable;
    }

    /**
     * 圆角矩形
     *
     * @param bgcolor Color.parseColor("#FFFFFF");
     * @param radio   (px)
     * @return
     */
    @SuppressLint("WrongConstant")
    public static GradientDrawable getRoundedRectangleDrawable(String bgcolor, int radio) {
        GradientDrawable drawable = getRectangleDrawable(bgcolor);
        drawable.setCornerRadius(radio);
        return drawable;
    }

    /**
     * 圆角矩形
     *
     * @param bgcolor Color.parseColor("#FFFFFF");
     * @param radios
     * @return
     */
    @SuppressLint("WrongConstant")
    public static GradientDrawable getRoundedRectangleDrawable(String bgcolor, float[] radios) {
        GradientDrawable drawable = getRectangleDrawable(bgcolor);
        drawable.setCornerRadii(radios);
        return drawable;
    }

    /**
     * 圆角矩形，带描边
     *
     * @param bgcolor Color.parseColor("#FFFFFF");
     * @param radio   (px)
     * @return
     */
    @SuppressLint("WrongConstant")
    public static GradientDrawable getRoundedRectangleDrawableWithStroke(String bgcolor, int radio,
                                                                         int lineColor, int lineWidth) {
        GradientDrawable drawable = getRectangleDrawable(bgcolor);
        drawable.setCornerRadius(radio);
        drawable.setStroke(lineWidth, lineColor);
        return drawable;
    }

    /****************StateListDrawable = <selector></selector>********************/
    /**
     *
     * @param pressedColor
     * @param checkedColor
     * @param selectedColor
     * @param focusedColor
     * @param normalColor
     * @return
     */
    public static StateListDrawable getStateListDrawable(String pressedColor,
                                                         String checkedColor,
                                                         String selectedColor,
                                                         String focusedColor,
                                                         String normalColor) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(new int[]{android.R.attr.state_pressed}, getRectangleDrawable(pressedColor));
        drawable.addState(new int[]{android.R.attr.state_checked}, getRectangleDrawable(checkedColor));
        drawable.addState(new int[]{android.R.attr.state_selected}, getRectangleDrawable(selectedColor));
        drawable.addState(new int[]{android.R.attr.state_focused}, getRectangleDrawable(focusedColor));
        drawable.addState(new int[]{}, getRectangleDrawable(normalColor));//默认状态
        return drawable;
    }

}
